<template>
  <div class="login">

    <el-form ref="loginRef" :rules="loginRules" :model="loginForm"  class="login-form">
      <div>
        <img src="../assets/image/image2.png" style="width: 75px;height: 75px;position: absolute;bottom: 0;left: 0;" alt="">
      </div>
      <h3 class="title" style="font-weight: bold;">后台管理系统</h3>

      <el-form-item prop="username">
        <el-input
            v-model="loginForm.username"
        type="text"
        class="w-50 m-2"
        placeholder="请输入账号"
        :prefix-icon="User"
        :clearable="true"
      />


      </el-form-item>
      <el-form-item prop="password">
        <el-input
            v-model="loginForm.password"
            type="password"
            size="large"
            auto-complete="off"
            placeholder="请输入密码"
            :prefix-icon="Key"
            :show-password="true"
            :clearable="true"
        >
        </el-input>
      </el-form-item>
      <el-checkbox v-model="loginForm.rememberMe"  style="margin:0 0 25px 0;">记住密码</el-checkbox>
      <el-form-item style="width:100%;margin-left: 25%;">
        <el-button
            size="large"
            type="primary"
            style="width:50%;"
            @click.prevent="handleLogin"
        >
          <span>登 录</span>
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import {ref} from "vue";
import {Key, User,View,Hide} from "@element-plus/icons-vue";
import requestUtil from "@/utils/request";
import qs from 'qs'
import { ElMessage } from 'element-plus'
// jscookie和加密解密工具类
import Cookies from "js-cookie";
import { encrypt, decrypt } from "@/utils/jsencrypt";
import router from "@/router";
const loginForm = ref({
  username:'',
  password:'',
  rememberMe:false
})

const loginRef = ref('')

// 空值校验
const loginRules = {
  username: [{required: true, trigger: "blur", message: "请输入您的账号"}],
  password: [{required: true, trigger: "blur", message: "请输入您的密码"}]
};


// 登录验证
const handleLogin = async()=>{
  loginRef.value.validate(async(valid)=>{
    if(valid){
     const result = await requestUtil.post('user/login?'+qs.stringify(loginForm.value))
   
    console.log(result)
     if(result.data.code === '200'){
        ElMessage({
          message: result.data.info,
          type: 'success',
        })
        // 记住密码功能
        // 当勾选了需要记住密码后，将用户名和密码以及记住密码的状态存储到cookie中，对密码进行加密
        if (loginForm.value.rememberMe) {
          // 设置过期时间30天
          Cookies.set("username", loginForm.value.username, { expires: 30 });
          Cookies.set("password", encrypt(loginForm.value.password), { expires: 30 });
          Cookies.set("rememberMe", loginForm.value.rememberMe, { expires: 30 });
        } else {
          // 否则移除
          Cookies.remove("username");
          Cookies.remove("password");
          Cookies.remove("rememberMe");
        }
         // 登录成功后，绑定token和用户信息
         sessionStorage.setItem('token',result.data.token)
         const currentUser = result.data.user
         currentUser.roles = result.data.roles
         sessionStorage.setItem('userInfo',JSON.stringify(currentUser))
         //  登录成功后，存储权限信息
         sessionStorage.setItem('menuList',JSON.stringify(result.data.menuList))
         // 跳转首页
         router.replace('/')
     }else{
      ElMessage.error(result.data.info)
     }
    }else{
      console.log('请求失败')
    }
  })
}
// 进入页面获取cookie存储的信息，若没有则赋值为空，若存在数据则直接将数据附上去
function getCookie() {
  // 获取存储的cookie信息
  const username = Cookies.get("username");
  const password = Cookies.get("password");
  const rememberMe = Cookies.get("rememberMe");
  // 设置表单的内容
  loginForm.value = {
    username: username === undefined ? loginForm.value.username : username,
    // 对密码解密并重新赋值
    password: password === undefined ? loginForm.value.password : decrypt(password),
    rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
  };
}
// 执行函数
getCookie();
</script>

<style lang="scss" scoped>
a{
  color:white
}
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-image: url('@/assets/styles/images/bg.jpg');
  background-size: cover;
}
.title {
  margin: 0 auto 30px auto;
  text-align: center;
  color: #707070;
}

.login-form {
  border-radius: 6px;
  background: #ffffff;
  width: 400px;
  padding: 25px 25px 5px 25px;
  background:#ffffff url('../assets/image/image1.png') no-repeat right bottom;
  background-size: 80px 80px;
  position: relative;
  .el-input {
    height: 40px;
    input {
      display: inline-block;
      height: 40px;
    }
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 0;
  }

}
.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.login-code {
  width: 33%;
  height: 40px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.login-code-img {
  height: 40px;
  padding-left: 12px;
}
</style>
